package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.model.mongo.RecommendUser;
import com.tanhua.model.mongo.UserLike;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;


/**
 * @author ck
 * @date 2021/11/12 14:16
 */
@DubboService
public class RecommendUserApiImpl implements RecommendUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    //查询今日佳人
    @Override
    public RecommendUser queryWithMaxScore(Long toUserId) {

        //1、根据toUserId查询，根据评分score排序，获取第一条数据

        //构建Criteria
        //构建Query
        Query query = Query.query(Criteria.where("toUserId").is(toUserId))
                .with(Sort.by(Sort.Order.desc("score")))
                .limit(1);
        //查询
        return mongoTemplate.findOne(query, RecommendUser.class);
    }

    //分页查询
    @Override
    public PageResult queryRecommendUserList(Integer page, Integer pagesize, Long toUserId) {
        //1、构建Criteria对象
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        //2、创建Query对象
        Query query = Query.query(criteria);
        //3、查询数据总数
        Long count = mongoTemplate.count(query, RecommendUser.class);
        //4、调用mongoTemplate查询数据列表
        query = Query.query(criteria).with(Sort.by(Sort.Order.desc("score"))).limit(pagesize).skip((page - 1) * pagesize);
        List<RecommendUser> recommendUsers = mongoTemplate.find(query, RecommendUser.class);
        //5、构建返回值PageResult
        return  new PageResult(page,pagesize,count,recommendUsers);
    }

    /**
     * 查询两者的推荐数据
     * @param userId
     * @param userId1
     * @return
     */
    @Override
    public RecommendUser queryByUserId(Long userId, Long userId1) {
        Query query  = Query.query(Criteria.where("userId").is(userId)
                .and("toUserId").is(userId1));
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        if(recommendUser == null) {
            recommendUser = new RecommendUser();
            recommendUser.setUserId(userId);
            recommendUser.setToUserId(userId1);
            //构建缘分值
            recommendUser.setScore(95d);
        }
        return recommendUser;
    }

    /**
     * 查询探花列表，查询时需要排除喜欢和不喜欢的用户
     * 1、排除喜欢，不喜欢的用户
     * 2、随机展示
     * 3、指定数量
     */
    @Override
    public List<RecommendUser> queryCardsList(Long userId, int i) {
        //1、查询喜欢不喜欢的用户ID
        List<UserLike> userLikes = mongoTemplate.find(Query.query(Criteria.where("userId").is(userId)), UserLike.class);
        List<Long> likeUserIds = CollUtil.getFieldValues(userLikes, "likeUserId", Long.class);
        likeUserIds.add(userId);
        //2、构造查询推荐用户的条件
        //nin代表查询所有但是要排除某些数据
        Criteria criteria = Criteria.where("toUserId").is(userId).and("userId").nin(likeUserIds);
        //3、使用统计函数，随机获取推荐的用户列表
        //match用来指定查询条件
        //sample根据指定的条件获取随机的数量
        TypedAggregation<RecommendUser> recommendUserTypedAggregation = TypedAggregation.newAggregation(RecommendUser.class
                ,Aggregation.match(criteria),
                Aggregation.sample(i));
        AggregationResults<RecommendUser> results = mongoTemplate.aggregate(recommendUserTypedAggregation, RecommendUser.class);
        //4、构造返回
        return results.getMappedResults();

    }


}
